// 监听来自content-script的消息
chrome.runtime.onMessage.addListener(function (req, sender, back) {
    console.log(req, sender, back, "get from content.js");
    if (req.bookmarks) {
        chrome.bookmarks.getTree(res => {
            back(res[0].children)
        })
    }
});
$("#bookmarks").on("click", e => {
    chrome.bookmarks.getTree(res => {
        console.log(res[0].children)
        send("getbookmarks",res[0].children)
    })
})
$(function(){
    get_store("auto",res=>{
        if(res){
            console.log(res,"resresrs");
            $('#open')
        }else{
            set_store("auto",false)
        }
    })
})
$(".close").click(e=>{
    
})
$("#bookmarks_net").on("click", e => {
    $.ajax({
        url: 'http://106.52.76.72/chromePlugin/bookmarks',
        method: 'get',
        success: e => {
            send("getbookmarks_net",e.data)
        },
        error: e => {
            console.log(e, "ffffffffff")
        }
    })
})
$("#push_bookmarks").on("click", e => {
    btn_bookmarks(true)
})
$("#pull_bookmarks").on("click", e => {
    btn_bookmarks(false)
})
function btn_bookmarks(isSet){
    get_bookmarks(e => {
        if (e) {
            $.ajax({
                url: 'http://106.52.76.72/chromePlugin/bookmarks',
                method: 'post',
                data: {
                    data: JSON.stringify(e),
                    set: isSet?'1':'0'
                },
                success: e => {
                    if (e.code == 0) {
                        console.log(e.msg)
                    } else if (e.code == 3) {
                        console.log(e.msg)
                    }
                },
                error: e => {
                    console.log(e, "ffffffffff")
                }
            })
        }
    })
}